package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yxzr.youyou.adapter.LeaveMessageAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.LeaveMessageBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LeaveMessageActivity extends BaseActivity {
    private TextView comment_tv_send;
    private EditText comment_et_content;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mRefresh;
    private LeaveMessageAdapter adapter;
    private SharedPreferencesHelper sp;
    private int currIndex = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leavemessage);
        setTitleWithBack();
        sp = new SharedPreferencesHelper(this,"logindata");
        initView();
    }
    private void initView() {
        mRecyclerView = findViewById(R.id.comment_rv);
        mRefresh = findViewById(R.id.comment_refreshLayout);
        comment_tv_send = findViewById(R.id.comment_tv_send);
        comment_et_content = findViewById(R.id.comment_et_content);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(2,1));
        adapter = new LeaveMessageAdapter(this);
        mRecyclerView.setAdapter(adapter);

        //下拉刷新
        mRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                currIndex = 1;
                initData();
            }
        });
        //上提加载
        mRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                currIndex ++;
                initData();
            }
        });

        comment_tv_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                comment();

            }
        });
        initData();
    }


    private void initData(){
        RequestUtil.getInstence().API().leavingList((String) sp.getSharedPreference("token",""),currIndex+"","10")
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        LeaveMessageBean bean = gson.fromJson(t,LeaveMessageBean.class);

                        if (currIndex == 1) adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {
                        if (currIndex == 1){
                            mRefresh.finishRefresh();
                        }else {
                            mRefresh.finishLoadMore();
                        }
                    }

                    @Override
                    protected void onErr(Throwable e) {
                        if (currIndex == 1){
                            mRefresh.finishRefresh();
                        }else {
                            mRefresh.finishLoadMore();
                        }
                    }
                });
    }

    private void comment(){
        if (TextUtils.isEmpty(comment_et_content.getText().toString().trim())){
            Toast.makeText(this,"内容不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        RequestUtil.getInstence().API().addLeavingMessage((String) sp.getSharedPreference("token",""),
                comment_et_content.getText().toString().trim())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Toast.makeText(LeaveMessageActivity.this,"留言成功",Toast.LENGTH_LONG).show();
                        comment_et_content.setText("");
                        currIndex = 1;
                        initData();
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
